local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_catch_game"] or {}
end
------------------------------------------------------------------------------------------------------

local function player_catch_fn(player,_prefab_name)
    if not player:HasTag("playerghost") then
        ----------------------------------------------------------------------
        ----- 0.5% 的概率杀死玩家
        if math.random(1000) <= 5 then
            if player.components.health then
                    player.npc_base_lib:Player_Killed_With_Announce(GetStringsTable().kill_announce,"npc_event_catch_game")
                return               
            end
        end
        ----------------------------------------------------------------------
        local gift_num = math.random(4)
        if TUNING.NPC_CONFIG.REWARDS_LESS == true then
            gift_num = 1
        end
        if _prefab_name == "goldnugget" then
            gift_num = 30
        end

        player.npc_base_lib:GiveItemByName(_prefab_name or "log",gift_num)
        ----------------------------------------------------------------------
    end
end

local function Game_Task_Start(inst)
    local Base_Point = Vector3( inst.Transform:GetWorldPosition() )

    local item_name = "log"
    local temp_probability = math.random(1000)/10
    if temp_probability <= 5 then
        item_name = inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Special_Materials())
    elseif temp_probability < 35  then
        item_name = inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Cooked_Food_List())
    else
        item_name = inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Base_Food_List())
    end
    if item_name == "wobster_sheller_land" then
        item_name = "goldnugget"
    end

    local pt = inst.npc_base_lib:GetSpawnPoint(Base_Point,math.random(95)/10) or Base_Point
    SpawnPrefab("npc_item_catch_game_falling_object"):PushEvent("Set",{
        pt = pt,
        name = item_name,
        height = 15,
        fn = player_catch_fn
    })


end

local function GameStart_Entity()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()
    inst.entity:AddLight()

    inst.Light:SetRadius(10)
    -- inst.Light:SetIntensity(0.55)
    inst.Light:SetIntensity(0.7)
    -- inst.Light:SetFalloff(1.3)
    inst.Light:SetFalloff(2)
    inst.Light:SetColour(251/255, 240/255, 218/255)
    inst.Light:Enable(true)

    
    inst:AddTag("INLIMBO")
    inst:AddTag("FX")
    inst:AddTag("NOBLOCK")


    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    local time = 0.5
    inst:DoPeriodicTask(time,function()
        if inst:IsValid() then
            Game_Task_Start(inst)
        end
        if inst.npc_everything_data:Add("time",time) >= 100 then
            inst:Remove()
        end
    end)
    
    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)

    return inst
end

------------------------------------------------------------------------------------------------------
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    inst.AnimState:SetBank("gift")
    inst.AnimState:SetBuild("gift")

    MakeInventoryPhysics(inst)
    MakeHauntableLaunch(inst)
    inst.entity:SetPristine()


    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")

    inst:AddComponent("inspectable") --可检查组件
    
    inst:AddComponent("inventoryitem")
    -- inst.components.inventoryitem:ChangeImageName("wereitem_beaver")
	inst.components.inventoryitem.cangoincontainer = true


    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().catch_game_gift_box_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_catch_game_gift_box")] = GetStringsTable().inspect_str  --人物检查的描述

    ----------------- 设置随机外观
    inst:DoTaskInTime(0,function()
        local Skin_table = {
            [1] = {"idle_large1","gift_large1"},
            [2] = {"idle_large2","gift_large2"},
            [3] = {"idle_medium1","gift_medium1"},
            [4] = {"idle_medium2","gift_medium2"},
            [5] = {"idle_small1","gift_small1"},
            [6] = {"idle_small2","gift_small2"}
        }

        local ret_skin_table = Skin_table[math.random(#Skin_table)]
        inst.AnimState:PlayAnimation(ret_skin_table[1])
        inst.components.inventoryitem:ChangeImageName(ret_skin_table[2])
    end)

    local function OnUnwrapped(inst, pos, doer)
        local x,y,z = inst.Transform:GetWorldPosition()
        local task = SpawnPrefab("npc_item_catch_game_start")
        task.Transform:SetPosition(x,0,z)
        task.Ready = true
        inst:Remove()
    end
    inst:AddComponent("unwrappable")
    -- inst.components.unwrappable:SetOnWrappedFn(OnWrapped)
    inst.components.unwrappable:SetOnUnwrappedFn(OnUnwrapped)



    return inst
end

return Prefab("npc_item_catch_game_gift_box", fn),
        Prefab("npc_item_catch_game_start",GameStart_Entity)